package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.GarageService;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Garage activity
 * @author Andrei Senchuk
 */
public class GarageActivity extends ListActivity {
    private static final String CAR_DETAILS_ACTION = "net.taviscaron.drivenotes.android.CAR_DETAILS";
    
    private CarListAdapter adapter;
    private GarageService garageService;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        adapter = new CarListAdapter();
        setListAdapter(adapter);
        getListView().setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
		showCarDetails(position);
	    }
            
	});
        
        garageService = new GarageService(this);
    }
    
    @Override
    protected void onStart() {
	super.onStart();
	if(adapter.getCars() == null) {
	    if(garageService.isOnline()) {
		new GarageLoader().execute();
	    } else {
		Toast.makeText(this, "You need an internet connection", Toast.LENGTH_SHORT).show();
	    }
	}
    }
    
    private void showCarDetails(int position) {
	Car car = adapter.getItem(position);
	Intent intent = new Intent(CAR_DETAILS_ACTION);
	intent.putExtra("car", car);
	startActivity(intent);
    }
    
    private class CarListAdapter extends BaseAdapter {
	private Car[] cars;
	
	public Car[] getCars() {
	    return cars;
	}
	
	public void setCars(Car[] cars) {
	    this.cars = cars;
	    notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
	    return cars != null ? cars.length : 0;
	}

	@Override
	public Car getItem(int position) {
	    return cars[position];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
	    if(view == null) {
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.garage_item, null);
	    }
	    
	    Car car = getItem(position);

	    String engineType;
	    switch (car.getEngineType()) {
	    case DIESEL:
		engineType = "diesel";
		break;
	    case PETROL:
		engineType = "petrol";
		break;
	    case PETROL_GAS:
		engineType = "petrol-gas";
		break;
	    default:
		engineType = "";
		break;
	    }
	    
	    ((TextView)view.findViewById(R.id.carName)).setText(String.format("%s %s %d %s", car.getBrand(), car.getModel(), car.getEngineVolume(), engineType));
	    ((TextView)view.findViewById(R.id.carDescription)).setText(String.format("Year: %d; Mileage: %d", car.getYear(), car.getOdometrValue()));

	    return view;
	}
    }
    
    private class GarageLoader extends AsyncTask<Void, Void, Car[]> {
	private ProgressDialog dialog;
	@Override
	protected Car[] doInBackground(Void... args) {
	    return garageService.loadCars();
	}
	
	@Override
	protected void onPreExecute() {
	    dialog = ProgressDialog.show(GarageActivity.this, "", "Loading...");
	}
	
	@Override
	protected void onPostExecute(Car[] result) {
	    dialog.dismiss();
	    if(result == null || result.length == 0) {
		Toast.makeText(GarageActivity.this, "There is no cars in garage", Toast.LENGTH_LONG).show();
	    } else {
		adapter.setCars(result);
	    }
	}
    }
}
